package com.szgr.baoyin.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.szgr.baoyin.MyApplication;
import com.szgr.baoyin.R;
import com.szgr.baoyin.activity.AddProtectAddressActivity;
import com.szgr.baoyin.model.ProtectAddress;
import com.szgr.baoyin.utils.Constant;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yang on 15/11/5.
 */
public class ProtectAddressListAdapter extends BaseAdapter implements View.OnClickListener {
    private Context mContext;
    private List<ProtectAddress> data;
    public ProtectAddressListAdapter(Context context,List<ProtectAddress> data){
        mContext = context;
        this.data = data;
    }
    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if(convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_protect_address_list_view,parent,false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        holder = (ViewHolder) convertView.getTag();
        ProtectAddress protectAddress = data.get(position);
        holder.tv_company.setText(protectAddress.getGsmc());
        holder.tv_faren.setText(protectAddress.getGsfzr());
        holder.tv_farenPhone.setText(protectAddress.getGsfzr_tel());
        holder.tv_shengchan.setText(protectAddress.getJsfzr());
        holder.tv_shengchanPhone.setText(protectAddress.getJsfzr_tel());
        holder.tv_address.setText(protectAddress.getArea_info() + protectAddress.getAddress());
        holder.img_delete.setTag(position);
        holder.img_delete.setOnClickListener(this);
        holder.img_edit.setTag(position);
        holder.img_edit.setOnClickListener(this);
        return convertView;
    }

    public void setData(List<ProtectAddress> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        int tag = (int) v.getTag();
        ProtectAddress protectAddress = data.get(tag);
        switch (v.getId()){
            case R.id.item_delete:
                deleteAddress(protectAddress.getAddress_id(),tag);
                break;
            case R.id.item_icon:
                Intent intent = new Intent(mContext, AddProtectAddressActivity.class);
                intent.putExtra("address",protectAddress);
                mContext.startActivity(intent);
                break;
        }
    }

    private void deleteAddress(final String address_id, final int tag) {
        StringRequest sr = new StringRequest(Request.Method.POST, Constant.URL_DELETE_PROTECT_ADDRESS,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {
                        System.out.println("----delete--->" + s);
                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            String datas = jsonObject.getString("datas");
                            if("1".equals(datas)){
                                data.remove(tag);
                                notifyDataSetChanged();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> map = new HashMap<>();
                map.put("key",MyApplication.getmInstance().getKey());
                map.put("address_id",address_id);
                return map;
            }
        };
        MyApplication.getmInstance().addToRequestQueue(sr);
    }

    class ViewHolder{
        TextView tv_company;
        TextView tv_faren;
        TextView tv_farenPhone;
        TextView tv_shengchan;
        TextView tv_shengchanPhone;
        TextView tv_address;
        ImageView img_delete;
        ImageView img_edit;

        public ViewHolder(View view){
            tv_company = (TextView) view.findViewById(R.id.protect_address_company);
            tv_faren = (TextView) view.findViewById(R.id.protect_address_faren);
            tv_farenPhone = (TextView) view.findViewById(R.id.protect_address_faren_phone);
            tv_shengchan = (TextView) view.findViewById(R.id.protect_address_shengchan);
            tv_shengchanPhone = (TextView) view.findViewById(R.id.protect_address_shengchan_phone);
            tv_address = (TextView) view.findViewById(R.id.protect_address_address);
            img_delete = (ImageView) view.findViewById(R.id.item_delete);
            img_edit = (ImageView) view.findViewById(R.id.item_icon);
        }
    }
}
